import pandas as pd
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
OUTPUT_DIR = os.path.join(BASE_DIR,"outputs")
SOURCER_DIR = os.path.join(BASE_DIR,"sources")

def get_allfiles_path(path):
    Absolute_path = []
    a = []
    b = []
    for dirpath, dirnames, filenames in os.walk(path):  
        a.append(os.path.join(dirpath))
        b.append(filenames)
    for i in a:                      
        dir_list = os.listdir(i)    
        for j in dir_list:
            sub_dir = os.path.join(i, j)
            if os.path.isdir(sub_dir):    
                pass
            else:  
                Absolute_path.append(sub_dir)
    return(Absolute_path,b)[0]

# 筛选 df 表中，column_name 列包含字符串 string 的行
def include_str(df, column_name, string):
    return df[-df[column_name].str.find(string, start=0, end=None)>=0]

# 筛选 df 表中，column_name 列值大于 number 的行
def bigger_than(df, column_name, number):
    return df[df[column_name] > number]

# 筛选 df 表中，column_name 列值小于 number 的行
def less_than(df, column_name, number):
    return df[df[column_name] < number]

def unit(sourcefile, outputname):
    df = pd.read_csv(sourcefile, low_memory=False)
    df = df.eval("单票重量 = 实际重量 / 件数")
    df = df[df["单票重量"] >5]
    df = df[df["标识类别"].isin(["A标"])]
    #df = include_str(df, "托寄物品名", "电脑")
    #df = include_str(df, "托寄物品名", "机器人")
    #df = include_str(df, "托寄物品名", "主机")
    #df = include_str(df, "托寄物品名", "音响")
    #df = include_str(df, "托寄物品名", "平板")
    #df = include_str(df, "托寄物品名", "ipad")
    #df = include_str(df, "托寄物品名", "笔记本")
    #df = include_str(df, "托寄物品名", "iPad")
    #df = include_str(df, "托寄物品名", "MacBook")
    #df = include_str(df, "托寄物品名", "Macbook")
    #df = include_str(df, "托寄物品名", "macBook")
    #df = include_str(df, "托寄物品名", "macbook")
    #df = include_str(df, "托寄物品名", "Matebook")
    #df = include_str(df, "托寄物品名", "matebook")
    #df = include_str(df, "托寄物品名", "MateBook")
    #df = include_str(df, "托寄物品名", "iMac")
    #df = include_str(df, "托寄物品名", "照相机")
    #df = include_str(df, "托寄物品名", "摄像机")
    #df = include_str(df, "托寄物品名", "RedmiBook")
    #df = include_str(df, "托寄物品名", "打印机")
    #df = include_str(df, "托寄物品名", "导航")
    #df = include_str(df, "托寄物品名", "GPS")
    #df = include_str(df, "托寄物品名", "gps")
    #df = include_str(df, "托寄物品名", "Gps")
    #df = include_str(df, "托寄物品名", "新手机")
    #df = include_str(df, "托寄物品名", "行车记录仪")
    #df = include_str(df, "托寄物品名", "手表")
    #df = include_str(df, "托寄物品名", "蓝牙耳机")
    #df = include_str(df, "托寄物品名", "蓝牙音箱")
    #df = include_str(df, "托寄物品名", "电子烟")
    #df = include_str(df, "托寄物品名", "筋膜枪")
    #df = include_str(df, "托寄物品名", "相机")
    #df = include_str(df, "托寄物品名", "ETC")
    #df = include_str(df, "托寄物品名", "etc")
    #df = include_str(df, "托寄物品名", "电子秤")
    #df = include_str(df, "托寄物品名", "显示器")
    #df = include_str(df, "托寄物品名", "联想ThinkPad")
    #df = include_str(df, "托寄物品名", "Xbox")
    #df = include_str(df, "托寄物品名", "xbox")
 
    #df = bigger_than(df, "实际重量", 5)
    
    df.to_excel(outputname)

sources = get_allfiles_path(SOURCER_DIR)

for file in sources:
    print("The file currently being processed is: "+file)
    print("Please wait...")
    unit(file,os.path.join(OUTPUT_DIR,os.path.basename(file)+'.xlsx'))
    print("All down!")